/*<div class="zs-list">
	<p>多得多地点多多多多</p>
	<span class="float-right">10000</span>
	<span class="fa fa-heart-o float-right sc"></span>
	<div class="pic">
		<img src="<%=path %>/front/img/pic.jpg" alt="" />
	</div>
	<span class="s-title">打发打发</span>
	<span class="fa fa-tag sc float-left"></span>
	<span class="float-left zs-ll">公益</span>
	<span class="fa fa-eye sc float-left"></span>
	<span class="float-left">10000</span>
</div>*/
//计算汉字长度的方法
var jmz = {};
jmz.GetLength = function(str) {
  return str.replace(/[\u0391-\uFFE5]/g,"aa").length;  //先把中文替换成两个字节的英文，在计算长度
};
//当前页面载入
$("document").ready(function(){
	$(".zs-more").css("display","block");
	getDemo();
})
//绑定翻页
$(".zs-more").click(function(){
	var pgNo = Number($("#pgNo").val()) ;
	pgNo+=1;
	$("#pgNo").val(pgNo);
	getDemo();
});
//选择类型触发事件
function typeDemo(){
	$(".zs-more").css("display","block");
	$(".zzz-list").remove();
	$("#pgNo").val(1);
	//alert(	$(".zs-list").size());
	getDemo();
}
//搜索框搜索
function search(){
	$("#pgNo").val(1);
	$(".zs-more").css("display","block");
	$(".listIndex a").removeClass("a-choose");
	$("#industies a").eq(0).addClass("a-choose");
	$("#holidays a").eq(0).addClass("a-choose");
	$("#functions a").eq(0).addClass("a-choose");
	$(".zzz-list").remove();
	getDemo();
}

//排序处处理
$(".zs-fl #cs").click(function(){
	$("#pgNo").val(1);
	$(this).addClass('cs').siblings().removeClass('cs');
	$(".zs-more").css("display","block");
	$(".zzz-list").remove();
	getDemo();
});
/*//点击宣臻
$("#xz").click(function(){
	$(".zzz-list").remove();
	var planN = $("#planName").val();
	var planT = '';
	var industies = $("#industies .a-choose").attr("code");
	var holidays = $("#holidays .a-choose").attr("code");
	var functions = $("#functions .a-choose").attr("code");
	var pgNo=$("#pgNo").val();
	var pgSize = 9;
	var orderBy = $(".zs-fl #cs.cs").attr("code");
	planT = industies+','+holidays+','+functions;
	$.ajax({
		type:"GET",
		url:basePath+"/front/company.html",
		data:{planName:planN,planTag:planT,pageNo:pgNo,pageSize:pgSize,orderBy:orderBy},
		dataType:"json",
		success:function(data){
			var map = eval(data);
			var count = data.count;
			var list = data.list;
			buildCases(list);
			
			count = Math.ceil(count/9);
			if (count<=pgNo) {
				$(".zs-more").css("display","none");
			}
			$('.zs-list .pic').each(function(){
				$(this).click(function(){
					var planId = $(this).parent().find("#id").text();
					var count = $(this).parent().find("#watch").text();
					var num = Number(count);
					$(this).parent().find("#watch").text(num+1);
					$.ajax({
						type:"POST",
						url:basePath+"/front/addWatch.html",
						data:{planId:planId},
						dataType:"json",
						success:function(){
						},
						error:function(){
						}
					});
					buildContent($(this));
					$('.tc').show();
			    })			
			})
		},
		error:function(){
			alert("error");
		}
	});
})*/
//请求数据
function getDemo(){
	var planN = $("#planName").val();
	var planT = '';
	var industies = $("#industies .a-choose").attr("code");
	var holidays = $("#holidays .a-choose").attr("code");
	var functions = $("#functions .a-choose").attr("code");
	var pgNo=$("#pgNo").val();
	var pgSize = 9;
	var orderBy = $(".zs-fl .cs").attr("code");
	var company = '';
	if (orderBy == "xuanzhen") {
		orderBy="new";
		company="xuanzhen";
	}
	planT = industies+','+holidays+','+functions;
	$.ajax({
		type:"GET",
		url:basePath+"/inner/cases.html",
		data:{planName:planN,planTag:planT,pageNo:pgNo,pageSize:pgSize,orderBy:orderBy,company:company},
		dataType:"json",
		success:function(data){
			var map = eval(data);
			var count = data.count;
			var list = data.list;
			buildCases(list);
			count = Math.ceil(count/9);
			if (count<=pgNo) {
				$(".zs-more").css("display","none");
			}
			$('.zs-list .pic').each(function(){
				$(this).click(function(){
					var planId = $(this).parent().find("#id").text();
					var count = $(this).parent().find("#watch").text();
					var num = Number(count);
					$(this).parent().find("#watch").text(num+1);
					$.ajax({
						type:"POST",
						url:basePath+"/front/addWatch.html",
						data:{planId:planId},
						dataType:"json",
						success:function(){
						},
						error:function(){
						}
					});
					buildContent($(this));
					$('.tc').show();
			    })			
			})
		},
		error:function(){
			alert("error");
		}
	});
}
//生成HTML
function buildCases(data){
	var json = data;
	var html = "";
	$.each(
			json,
			function(i,item){
				var lengt = (item.planTag).split(",");
				var tag = "";
				for (var i = 0; i < lengt.length; i++) {
					if (lengt[i] == '') {
						lengt[i]=" ";
					}
					tag+=lengt[i]+'&nbsp; ';
				}
				var name = item.planName;
				if (jmz.GetLength(name)>24) {
					name = (item.planName).substr(0,12)+"...";
				}
				/*var like = parseFloat(item.planHot);
				if (like>10000) {
					like = Math.round(like/10000)+"W";
				}
				var watch = parseFloat(item.planWatch);
				if (watch>10000) {
					watch = Math.round(watch/10000)+"W";
				}*/
				html+="<div class='zs-list zzz-list'>";
				html+="<p>"+name+"</p>";
				html+="<p id = 'pName' style='display:none'>"+item.planName+"</p>";
				html+="<span class='float-right zan_no'>"+item.planHot+"</span>";
				if (item.planHot>0) {
					html+="<span class='fa fa-heart float-right sc zan' style='color:red;'></span>";
				}else{
					html+="<span class='fa fa-heart-o float-right sc zan'></span>";
				}
				html+="<div class='pic'><img src='"+basePath+"/file/getImg.html?saveAddress="+item.planCover+"' alt='' /></div>";
				var description = item.planDescription;
				if (jmz.GetLength(description)>50) {
					description = (item.planDescription).substr(0,25)+"...";
				}
				html+="<span class='s-title' >"+description+"</span>";
				html+="<span id='description' style='display:none'>"+item.planDescription+"</span>";
				html+="<span class='fa fa-tag sc float-left'></span>";
				html+="<span class='float-left zs-ll'>"+tag+"</span>";
				html+="<span class='float-right' id='watch'>"+item.planWatch+"</span>";
				html+="<span class='fa fa-eye sc float-right'></span>";
				html+="<span id='url' style='display:none' >"+item.planUrl+"</span>";
				html+="<span id='compnay' style='display:none' >"+item.planCompnay+"</span>";
				html+="<span id='id' style='display:none' >"+item.planId+"</span>";
				html+="<span id='planPics' style='display:none' >"+item.planPics+"</span>" +
						"<span id='planSuitPhone' style='display:none' >"+item.planSuitPhone+"</span></div>";
				
			}
	)
	$("#cases").append(html);
	bindClick();
}
//构建弹出层页面
function buildContent(data){
	var baseDoc = data.parent();
	var company = baseDoc.find("#compnay").text();
	var suit = Number(baseDoc.find("#planSuitPhone").text());
	var url = '';
	if (suit == 1) {
		url = "../warning.jsp";
	}else{
		url = baseDoc.find("#url").text();
	}
	var planPics = baseDoc.find("#planPics");
	var name = baseDoc.find("#pName").text();
	var hot = baseDoc.find(".zan_no").text();
	var img = baseDoc.find("img").attr("src");
	var description = baseDoc.find("#description").text();
	if (jmz.GetLength(description)>200) {
		description = description.substr(0,80)+"...";
	}
	var tag  = baseDoc.find(".float-left.zs-ll").text();
	var watch = baseDoc.find("#watch").text();
	var id = baseDoc.find("#id").text();
	var pics = baseDoc.find("#planPics").text();
	
	$(".tc iframe").attr("src",url);//链接
	$(".tc #name").text(name);//标题
	$(".tc #watch").text(watch);//观看次数
	$(".tc #hot").text(hot);//热度
	
	$(".tc #description").text(description).attr("title",baseDoc.find("#description").text());//描述
	$(".tc #company").text("出品："+company);//公司
	$(".tc #tag").text(tag);//标签
	$(".tc #imgSrc").attr("src","http://pan.baidu.com/share/qrcode?w=150&h=150&url="+url);
	$(".tc #id").val(id);//id
	var params = pics.split(",");
	var html = '';
	for(var i=0;i<params.length;i++){
		if (params[i]!='' & params[i]!=null & params[i]!='undefined') {
			html+="<a href='javascript:void(0);'><li>"
				+"<img src='"+basePath+"/file/getImg.html?saveAddress="+params[i]+"' alt='' />" 
				+"</li></a>"
		}
	}
	$("#pics a").remove();
	$("#pics").append(html);
	var scrollWidth=300*$('.only-scroll li').length-12;
	var leftWidth=scrollWidth-888;
	$('.only-scroll').width(scrollWidth);
	$('.btn-right').hover(function(){
		var nowLeft=parseInt($('.only-scroll').css('marginLeft'));
		var lastWidth=leftWidth+nowLeft;
		var timerLeft=lastWidth*10;
		$('.only-scroll').animate({marginLeft:-leftWidth},timerLeft,'linear');
		},function(){
		$('.only-scroll').stop();
	});
	$('.btn-left').hover(function(){
		var nowLeft=parseInt($('.only-scroll').css('marginLeft'));
		var timerRight=-nowLeft*10;
		$('.only-scroll').animate({marginLeft:0},timerRight,'linear');
		},function(){
		$('.only-scroll').stop();
	});
		$('.only-scroll').css({marginLeft:0});
		carousel()
}
function carousel() {
    var LILENGTH = ($('.only-scroll li').width() + 14) * $('.only-scroll li').length;
    var IMGWIDTH = $('.only-out').width();
    var scrollWidth= LILENGTH - 12; 
    var leftWidth= scrollWidth - IMGWIDTH;
    $('.only-scroll').width(scrollWidth);
    $('.btn-right').hover(function(){
        var nowLeft=parseInt($('.only-scroll').css('marginLeft'));
        var lastWidth= leftWidth+nowLeft;
        var timerLeft= lastWidth * 10;
        $('.only-scroll').animate({marginLeft:-leftWidth},timerLeft,'linear');
    },function(){
        $('.only-scroll').stop();
    });
    $('.btn-left').hover(function(){
        var nowLeft=parseInt($('.only-scroll').css('marginLeft'));
        var timerRight= -nowLeft * 10;
        $('.only-scroll').animate({marginLeft:0},timerRight,'linear');
    },function(){
        $('.only-scroll').stop();
    });
}
function bindClick(){
	//点赞
	$(".zan").click(function(){
		var _this =  $(this);
		var planId = _this.parent().find("#id").text();
		var count = _this.parent().find(".zan_no").text();
		var num = Number(count);
		if (!num>0) {
			_this.parent().find(".fa-heart-o ").addClass("fa-heart").css("color","red").removeClass("fa-heart-o");
		}
		$.ajax({
				type:"POST",
				url:basePath+"/front/addLike.html",
				data:{planId:planId},
				dataType:"json",
				success:function(json){
					_this.parent().find(".zan_no").text(num+1);
					if (json==1) {
						layer.open({
							content: '点赞成功！'
								,skin: 'msg'
									,time: 1 //2秒后自动关闭
						});
					}else{
						layer.open({
							content: '点赞失败！'
								,skin: 'msg'
									,time: 1 //2秒后自动关闭
						});
					}
				},
				error:function(){
					layer.open({
					    content: '点赞失败！'
					    ,skin: 'msg'
					    ,time: 1 //2秒后自动关闭
					  });
				}
		});
	})
/*	$("#watch").click(function(){
		var planId = $(this).parent().find("#id").text();
		var count = $(this).text();
		$.ajax({
				type:"POST",
				url:basePath+"/front/addWatch.html",
				data:{planId:planId},
				dataType:"json", 
				success:function(){
				},
				error:function(){
				}
		});
	})*/
}
